<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/9 0009
 * Time: 18:22
 * 99健康
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Jiankang extends CI_Controller
{
    /**
     * 获取科室对应疾病id
     */
    public function ids()
    {
        $dept1 = [
            1=>'内科',
            2=>'外科',
            3=>'妇产科',
            4=>'儿科',
            5=>'男科',
            6=>'皮肤性病科',
            7=>'五官科',
            8=>'肿瘤科',
            9=>'精神心理科',
            10=>'不孕不育',
            11=>'其他',
        ];
        foreach($dept1 as $k=>$d){
            $url = "https://www.99.com.cn/jibing/dise_".$k."_0.html";
            //$url = "https://www.baidu.com";
            $content = geturl($url);
            $html = str_get_html($content);
            $sql = "";
            foreach ($html->find('#deptwo', 0)->find('li') as $ak => $li) {
                if($ak == 0){
                    continue;
                }
                $a = $li->find('a',0);
                $href = $a->href;
                $dept1 = $d;
                $dept2 = trim($a->innertext);
                $sql .= "('".$dept1."','".$dept2."','".$href."'),";
            }
            $sql = substr($sql,0,-1);
            $sql = 'INSERT ignore INTO ai_39_dept(dept1,dept2,url) VALUES '.$sql;
            //echo $sql;exit;
            $this->db->query($sql);
        }
    }
    public function disease(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $id = $redis->lPop('jiankang_disease');
        if(!$id){
            echo "nodata";
            exit;
        }
        $info = $this->db->query("select * from ai_39_dept where id=".$id)->row_array();
        $url = "https://www.99.com.cn/".$info['url'];
        $content = geturl($url);
        $html = str_get_html($content);
        $sql = "";
        foreach ($html->find('.dse-nr', 0)->find('li') as $ak => $li) {
            $a = $li->find('a',0);
            $href = $a->href;
            $id = (int)str_replace(['/jibing/','.html'],'',$href);
            $dept1 = $info['dept1'];
            $dept2 = $info['dept2'];
            $disease_name = trim($a->innertext);
            $sql .= "(".$id.",'".$disease_name."','".$dept1."','".$dept2."','".$href."'),";
        }
        $sql = substr($sql,0,-1);
        $sql = 'INSERT ignore INTO ai_spider_39_disease(did,disease_name,department_1,department_2,url) VALUES '.$sql;
        $this->db->query($sql);
        echo 1;
    }

    public function disease_info(){

        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 从list拿出来一个，如果执行不成功，写到另外的list里
        $id = $redis->lPop('jiankang_disease_info');
        if(!$id){
            echo "nodata";
            exit;
        }
        $info = $this->db->query("select * from ai_spider_39_disease where id=".$id)->row_array();
        $url = "https://www.99.com.cn".$info['url'];
        $content = geturl($url);
        $html = str_get_html($content);
        $sql = "update ai_spider_39_disease set ";
        $fields = ['guahao','zhengzhuang','buwei','xiangguan','renqun','jiancha','zhiliao','shoushu','chuanran','yaopin','yichuan','feiyong'];
        foreach ($html->find('.dse-sleft', 0)->find('li') as $ak => $li) {
            if($li->find('a')){
                $des = "";
                foreach($li->find('a') as $a){
                    $des .= $a->innertext." ";
                }
            }else{
                $text = strip_tags($li->innertext);
                $texts = explode("：",$text);
                $des= str_replace(" ","",$texts[1]);
            }

            $sql .= $fields[$ak]."='".trim($des)."',";
        }
        $sql = substr($sql,0,-1);
        $sql .= " where id=".$id;
        $this->db->query($sql);
        echo 1;
    }

    public function test(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        for($i=71 ; $i<114 ; $i++){
            $redis->rPush('jiankang_disease_info',$i);
        }
    }

}